﻿

using System;
using AutoMapper;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;

using Oliving.Edge.AuthHelper;
using Oliving.Edge.Model;
using Oliving.Edge.Model.Models;
using Oliving.Edge.IServices;
using Oliving.Edge.Model.Dto;
using Oliving.Edge.IServices.WY;
using Oliving.Edge.Model.Dto.WY;
using Oliving.Edge.Model.Models.XLT;
using System.Linq.Expressions;
using Oliving.Edge.Common.Extensions;

namespace Oliving.Edge.Controllers
{
    [ApiController]
    [Route("api/WY_Project")]
    [Authorize(Permissions.Name)]
    public class WY_ProjectController : OlivingControll
    {
        IMapper _mapper;
        private readonly IWY_ProjectServices _WY_ProjectServices;
        private readonly ILogger<WY_ProjectController> _logger;


        public WY_ProjectController(
                IHttpContextAccessor accessor, PermissionRequirement requirement,
                IMapper mapper, ILogger<WY_ProjectController> logger,
                IWY_ProjectServices WY_ProjectServices
            ) : base(accessor, requirement)
        {
            _logger = logger;
            _mapper = mapper;
            _WY_ProjectServices = WY_ProjectServices;
        }



        [HttpGet]
        [AllowAnonymous]
        [Route("/api/[controller]/Get")]
        public async Task<MessageModel<WY_ProjectDto>> Get(string id)
        {
            var data = new MessageModel<WY_ProjectDto>();
            WY_Project model = await _WY_ProjectServices.QueryById(id);
            if (model == null)
            {
                data.response = null;
                data.msg = "数据不存在！";
                data.success = false;
            }
            else
            {
                var dto = _mapper.Map<WY_ProjectDto>(model);
                data.response = dto;
                data.msg = "获取成功！";
                data.success = true;
            }
            return data;
        }




        [HttpGet]
        [Route("/api/[controller]/GetPageList")]
        public async Task<MessageModel<PageModel<WY_ProjectPageDto>>> GetPageList(int page = 1, int PageSize = 20, string sidx = "", string order = "", string key = "")
        {
            Expression<Func<WY_Project, bool>> whereExpression = _WY_Project => true;
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }
            else
            {
                //   whereExpression = whereExpression.And(d =>
                //           d.Canning_Car.Contains(key) 
                //       );
            }


            whereExpression = whereExpression.And(d => d.DeleteMark == 0);



            var data = await _WY_ProjectServices.QueryPageList(whereExpression, page, PageSize, sidx, order);

            return new MessageModel<PageModel<WY_ProjectPageDto>>()
            {
                msg = "获取成功",
                success = true,
                response = data
            };
        }



        [HttpPut]
        [Route("/api/[controller]/Update")]
        public async Task<MessageModel<string>> Update([FromBody] WY_ProjectDto dto)
        {
            var data = new MessageModel<string>();

            if (dto != null && !string.IsNullOrEmpty(dto.Id))
            {
                WY_Project model = await _WY_ProjectServices.QueryById(dto.Id);
                if (model != null)
                {
                    model.ProName = dto.ProName;
                    model.ProAddress = dto.ProAddress;
                    model.PartyA = dto.PartyA;
                    model.PartyAUserName = dto.PartyAUserName;
                    model.PartyAPhone = dto.PartyAPhone;
                    model.ProUser = dto.ProUser;
                    model.Lat = dto.Lat;
                    model.Lng = dto.Lng;
                    model.ProRemark = dto.ProRemark;
                    model.SortCode = dto.SortCode;

                    model.EnabledMark = dto.EnabledMark;

                    model.ComID = dto.ComID;



                    model.Modify(_accessor, "");
                    data.success = await _WY_ProjectServices.Update(model);

                    if (data.success)
                    {
                        data.msg = "更新成功";
                        data.response = dto.Id.ObjToString();
                    }
                }
                else
                {
                    data.success = false;
                    data.msg = "参数异常，您要更新的数据不存在";
                }
            }
            else
            {
                data.success = false;
                data.msg = "参数异常";
            }
            return data;
        }





        [HttpPost]
        [Route("/api/[controller]/Add")]
        public async Task<MessageModel<string>> Add(WY_ProjectDto dto)
        {
            var data = new MessageModel<string>();
            WY_Project model = _mapper.Map<WY_Project>(dto);

            model.Create(_accessor);


            int id = await _WY_ProjectServices.AddGuid(model);

            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }
            return data;
        }




        [HttpDelete]
        [Route("/api/[controller]/Delete")]
        public async Task<MessageModel<string>> Delete(string id)
        {
            var data = new MessageModel<string>();
            if (!string.IsNullOrEmpty(id))
            {
                var userDetail = await _WY_ProjectServices.QueryById(id);
                userDetail.DeleteMark = 1;
                data.success = await _WY_ProjectServices.Update(userDetail);
                if (data.success)
                {
                    data.msg = "删除成功";
                    data.response = userDetail?.Id.ObjToString();
                }
            }
            return data;
        }





        [HttpDelete]
        [Route("/api/[controller]/BatchDelete")]
        public async Task<MessageModel<string>> BatchDelete(string ids)
        {
            var data = new MessageModel<string>();
            if (!string.IsNullOrEmpty(ids))
            {
                if (false)
                {
                    #region 软删除

                    int[] list = Array.ConvertAll<string, int>(ids.Split(','), s => int.Parse(s));

                    WY_Project model = new WY_Project();
                    //  model.C_State = -1;          ???????????????????
                    string whereStr = " Id in (" + ids + ")";
                    List<string> colList = new List<string>();
                    //  colList.Add("C_State");    ???????????????????
                    List<string> colHL = new List<string>();

                    bool isOK = await _WY_ProjectServices.Update(model, colList, colHL, whereStr);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }

                    #endregion
                }
                {
                    #region 直接删除 

                    object[] list = Array.ConvertAll<string, object>(ids.Split(','), s => int.Parse(s));

                    bool isOK = await _WY_ProjectServices.DeleteByIds(list);

                    data.success = isOK;
                    if (data.success)
                    {
                        data.msg = "删除成功";
                        data.response = list.Count().ObjToString();
                    }
                    #endregion
                }
            }
            return data;
        }




    }
}

